package com.bwie.service.Impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.poi.StringUtils;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Menu;
import com.bwie.mapper.MenuMapper;
import com.bwie.service.MenuService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public R getTreeMenu(PageInfoVo pageInfoVo) {
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            menuQueryWrapper.lambda().like(Menu::getMenuName,pageInfoVo.getKeyWord());
        }
        List<Menu> list = menuMapper.selectList(menuQueryWrapper);
        List<Tree<Integer>> build = TreeUtil.build(list, 0, (tree, treeNode) -> {
            treeNode.setId(tree.getMenuId());
            treeNode.setName(tree.getMenuName());
            treeNode.setParentId(tree.getPid());
            treeNode.putExtra("path", tree.getPath());
            treeNode.putExtra("status", tree.getStatus());
            treeNode.putExtra("createTime", tree.getCreateTime());
        });
        return R.OK(build);
    }
}
